//SPDX-FileCopyrightText: Copyright 2022-2024 深圳市同心圆网络有限公司
//SPDX-License-Identifier: GPL-3.0-only

package idea_dao

import (
	"context"

	"atomgit.com/openlinksaas/api-server/dao/db_ns"
	"atomgit.com/openlinksaas/proto-gen-go.git/project_idea_api"
	"go.mongodb.org/mongo-driver/bson"
	"go.mongodb.org/mongo-driver/mongo"
	"go.mongodb.org/mongo-driver/mongo/options"
)

type IdeaGroupDbItem struct {
	IdeaGroupId  string `bson:"_id"`
	Name         string `bson:"name"`
	ProjectId    string `bson:"projectId"`
	Weight       uint32 `bson:"weight"`
	IdeaCount    uint32 `bson:"ideaCount"`
	CreateUserId string `bson:"createUserId"`
	UpdateUserId string `bson:"updateUserId"`
	CreateTime   int64  `bson:"createTime"`
	UpdateTime   int64  `bson:"updateTime"`
}

func (item *IdeaGroupDbItem) ToIdeaGroup() *project_idea_api.IdeaGroup {
	return &project_idea_api.IdeaGroup{
		IdeaGroupId:  item.IdeaGroupId,
		Name:         item.Name,
		Weight:       item.Weight,
		IdeaCount:    item.IdeaCount,
		CreateUserId: item.CreateUserId,
		UpdateUserId: item.UpdateUserId,
		CreateTime:   item.CreateTime,
		UpdateTime:   item.UpdateTime,
	}
}

type _IdeaGroupDao struct {
	mc *mongo.Collection
}

func newIdeaGroupDao(db *mongo.Database) (*_IdeaGroupDao, error) {
	mc := db.Collection(db_ns.DB_NS_PROJECT_IDEA + "group")
	_, err := mc.Indexes().CreateOne(context.Background(), mongo.IndexModel{
		Keys: bson.D{{"projectId", 1}},
	})
	if err != nil {
		return nil, err
	}
	return &_IdeaGroupDao{mc: mc}, nil
}

func (db *_IdeaGroupDao) Insert(ctx context.Context, dbItem *IdeaGroupDbItem) error {
	_, err := db.mc.InsertOne(ctx, dbItem)
	return err
}

func (db *_IdeaGroupDao) UpdateNameAndWeight(ctx context.Context, projectId, ideaGroupId, name string, weight uint32, updateUserId string, updateTime int64) error {
	_, err := db.mc.UpdateOne(ctx, bson.M{"projectId": projectId, "_id": ideaGroupId},
		bson.M{"$set": bson.M{"name": name, "weight": weight, "updateUserId": updateUserId, "updateTime": updateTime}})
	return err
}

func (db *_IdeaGroupDao) UpdateIdeaCount(ctx context.Context, projectId, ideaGroupId string, count uint32) error {
	_, err := db.mc.UpdateOne(ctx, bson.M{"projectId": projectId, "_id": ideaGroupId},
		bson.M{"$set": bson.M{"ideaCount": count}})
	return err
}

func (db *_IdeaGroupDao) Exist(ctx context.Context, projectId, ideaGroupId string) (bool, error) {
	count, err := db.mc.CountDocuments(ctx, bson.M{"projectId": projectId, "_id": ideaGroupId})
	return count > 0, err
}

func (db *_IdeaGroupDao) Get(ctx context.Context, projectId, ideaGroupId string) (*IdeaGroupDbItem, error) {
	retItem := &IdeaGroupDbItem{}
	err := db.mc.FindOne(ctx, bson.M{"projectId": projectId, "_id": ideaGroupId}).Decode(retItem)
	return retItem, err
}

func (db *_IdeaGroupDao) Remove(ctx context.Context, projectId, ideaGroupId string) error {
	_, err := db.mc.DeleteOne(ctx, bson.M{"projectId": projectId, "_id": ideaGroupId})
	return err
}

func (db *_IdeaGroupDao) List(ctx context.Context, projectId string) ([]*IdeaGroupDbItem, error) {
	cursor, err := db.mc.Find(ctx, bson.M{"projectId": projectId}, options.Find().SetSort(bson.M{"weight": -1}))
	if err != nil {
		return nil, err
	}
	retList := []*IdeaGroupDbItem{}
	err = cursor.All(ctx, &retList)
	return retList, err
}
